package uit.minito.timduongxebuyt;

import java.util.ArrayList;
import java.util.Vector;

import uit.minito.model.MyData;
import uit.minito.model.MyPoint;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class DrawPathOverlay {

	Vector<MyData> arrData;
	Vector<LatLng> arrPoint = new Vector<LatLng>();
	int color;
	GoogleMap map;
	Context context;
	boolean isWalking = true;

	public DrawPathOverlay(GoogleMap _map, Context _context, Vector<MyData> _arrData, int _color, Boolean _isWalking) {
		arrData = _arrData;
		color = _color; 
		map = _map;
		context = _context;
		isWalking = _isWalking;
		
		for(int i = 0 ; i < arrData.size(); i ++) {
			arrPoint.add(arrData.get(i).getLatLng());
		}
	}
	
	public DrawPathOverlay(GoogleMap _map, Context _context, Vector<LatLng> _arrPoint, int _color) {
		color = _color; 
		map = _map;
		context = _context;
		arrPoint = _arrPoint;
	}
	
	public Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
		Resources resources = gContext.getResources();
		float scale = resources.getDisplayMetrics().density;
		Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

		android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
		// set default bitmap config if none
		if (bitmapConfig == null) {
			bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
		}
		// resource bitmaps are imutable,
		// so we need to convert it to mutable one
		bitmap = bitmap.copy(bitmapConfig, true);

		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		paint.setTextSize((int) (23 * scale));
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setStrokeWidth(1);
		// draw text to the Canvas center
		Rect bounds = new Rect();
		paint.getTextBounds(gText, 0, gText.length(), bounds);
		int x = (bitmap.getWidth() - bounds.width()) / 2 - 4;
		int y = (bitmap.getHeight() + bounds.height()) / 2 - 10;

		canvas.drawText(gText, x * scale, y * scale, paint);

		return bitmap;
	}

	Marker startMarker;
	Marker endMarker;
	Vector<Polyline> lines = new Vector<Polyline>();
	
	public void draw() {
		
		for (int i = 0, size = arrPoint.size(); i < size - 1; i++) {
			lines.add(map.addPolyline(new PolylineOptions().add(arrPoint.get(i), arrPoint.get(i + 1)).width(5).color(color)));
		}
		
		if (isWalking == false) {
			Bitmap textbm = drawTextToBitmap(context, R.drawable.backrouter, arrData.firstElement().getRouterID().substring(4));
		    startMarker = map.addMarker(new MarkerOptions().position(arrPoint.get(0))
		    		.title("Bắt trạm tuyến " + arrData.firstElement().getRouterID().substring(4) + "tại:")
			        .snippet(arrData.firstElement().getAddress())
					.icon(BitmapDescriptorFactory.fromBitmap(textbm)));
			
		    endMarker = map.addMarker(new MarkerOptions()
		        .position(arrPoint.lastElement())
		        .title("Xuống trạm tuyến " + arrData.firstElement().getRouterID().substring(4) + "tại:")
		        .snippet(arrData.lastElement().getAddress())
		        .icon(BitmapDescriptorFactory
		            .fromResource(R.drawable.locationicon)));
		}
	}
	
	public void clear() {
		if (isWalking == false){
			startMarker.remove();
			endMarker.remove();
		}
		
		for ( int i = 0; i < lines.size(); i ++) {
			lines.get(i).remove();
		}
		
		Log.i("CLear", "Done");
	}
}
